package com.hbt.orderfood.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.appcompat.app.AppCompatActivity;

import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.entitys.User;
import com.hbt.orderfood.utils.StringUtils;

import java.util.LinkedList;
import java.util.List;

public class DBAdapter {

    private final SQLiteDatabase db;

    public DBAdapter(Context context){
        SQLiteOpenHelper helper = new DBOpenHelper(context);
        db = new DBOpenHelper(context).getWritableDatabase();
    }

    public boolean hasFoodidInOrders(int food_id){
        List<Order> orders = getAllOrders();
        for (Order o : orders){
            if (o.getFood_id()==food_id){
                return true;
            }
        }
        return false;
    }

    public void  delOrderByUserID(String user_id){
        db.delete(OrderTable.TABLE_NAME, OrderTable.USER_ID+"=?", new String[]{user_id});
    }
    public User getUserByUsername(String username){
        Cursor c = db.query(UserTable.TABLE_NAME, null, UserTable.USERNAME+"=?", new String[]{username}, null,null, null);
        c.moveToFirst();
        return cursorConvertToUser(c);
    }

    public void insertAllOrders(List<Order> orders){
        for(Order o : orders){
            ContentValues values = orderConvertToValue(o);
            db.insert(OrderTable.TABLE_NAME, OrderTable.NUM, values);
        }
    }


    public List<User> getAllUsers(){
        List<User> users = new LinkedList<>();
        Cursor c = db.query(UserTable.TABLE_NAME, null, null, null, null, null,null);
        for (int i = 0;i < c.getCount();i++){
            c.moveToPosition(i);
            users.add(cursorConvertToUser(c));
        }
        return users;
    }

    public List<Order> getAllOrders(){
        List<Order> orders = new LinkedList<>();
        Cursor c = db.query(OrderTable.TABLE_NAME, null, null, null,null,null,null);
        for (int i = 0;i < c.getCount(); i++){
            c.moveToPosition(i);
            orders.add(cursorConvertToOrder(c));
        }
        return orders;
    }

    public User getUserByID(String user_id){
        Cursor c = db.query(UserTable.TABLE_NAME, null, UserTable._ID+"=? ", new String[]{user_id}, null,null,null);
        c.moveToPosition(0);
        return cursorConvertToUser(c);
    }

    Order cursorConvertToOrder(Cursor c){
        Order order = new Order();
        order.setUser_id(c.getInt(c.getColumnIndex(OrderTable.USER_ID)));
        order.setFood_id(c.getInt(c.getColumnIndex(OrderTable.FOOD_ID)));
        order.setNum(c.getInt(c.getColumnIndex(OrderTable.NUM)));
        order.setPrice(c.getInt(c.getColumnIndex(OrderTable.PRICE)));
        return order;
    }

    User cursorConvertToUser(Cursor c){
        if (c.getCount()==0){
            return null;
        }
        User user = new User();
        user.setId(c.getInt(c.getColumnIndex(UserTable._ID)));
        user.setUsername(c.getString(c.getColumnIndex(UserTable.USERNAME)));
        user.setPassword(c.getString(c.getColumnIndex(UserTable.PASSWORD)));
        return user;
    }

    public List<Order> getOrdersByUserIDAndFoodID(String user_id, String food_id){
        Cursor c = db.query(OrderTable.TABLE_NAME, null,OrderTable.USER_ID+"=? "+OrderTable.FOOD_ID+"=?", new String[]{user_id, food_id},null, null, null);
        return null;
    }

    public List<Order> getOrdersByUserID(String user_id){
        List<Order> orders = new LinkedList<>();
        Cursor c = db.query(OrderTable.TABLE_NAME, null, OrderTable.USER_ID+"=?", new String[]{user_id}, null, null, null);
        for (int i = 0; i < c.getCount(); i++){
            c.moveToPosition(i);
            Order order = cursorConvertToOrder(c);
            orders.add(order);
        }
        return orders;
    }

    public void insertUser(User user){
        db.insert(UserTable.TABLE_NAME, UserTable.PASSWORD, userConvertToValues(user));
    }

    ContentValues userConvertToValues(User user){
        ContentValues values = new ContentValues();
        values.put(UserTable.USERNAME, user.getUsername());
        values.put(UserTable.PASSWORD, user.getPassword());
        return values;
    }


    public ContentValues orderConvertToValue(Order order){
        ContentValues values = new ContentValues();
        values.put(OrderTable.USER_ID, order.getUser_id());
        values.put(OrderTable.FOOD_ID, order.getFood_id());
        values.put(OrderTable.NUM, order.getNum());
        values.put(OrderTable.PRICE, order.getPrice());
        return values;
    }

    public Food getFoodByFoodID(String food_id){
        Cursor c = db.query(FoodTable.TABLE_NAME, null, FoodTable._ID+"=?", new String[]{food_id}, null, null, null);
        return CurcorConvertToFood(c).get(0);
    }

    public void deleteFoodByFoodID(int food_id){
        db.delete(FoodTable.TABLE_NAME, FoodTable._ID+"=?", new String[]{String.valueOf(food_id)});
    }

    public void updateFoodByFoodID(String food_id, Food food_new){
        db.update(FoodTable.TABLE_NAME, foodConvertToValue(food_new),FoodTable._ID+"=?", new String[]{food_id});
    }

    public List<Food> getFoodsByType(String type){
        Cursor c = db.query(FoodTable.TABLE_NAME, null, FoodTable.TYPE+"=?",new String[]{type}, null, null, null );
        return CurcorConvertToFood(c);
    }
    public List<Food> CurcorConvertToFood(Cursor c){
        List<Food> foods = new LinkedList<>();
        for (int i = 0;i < c.getCount();i++){
            c.moveToPosition(i);
            Food food = new Food();
            String name = c.getString(c.getColumnIndex(FoodTable.NAME));
            int id = c.getInt(c.getColumnIndex(FoodTable._ID));
            String type = c.getString(c.getColumnIndex(FoodTable.TYPE));
            int price = Integer.parseInt(c.getString(c.getColumnIndex(FoodTable.PRICE)));
            food.setName(name);
            food.setId(id);
            food.setType(type);
            food.setPrice(price);
            foods.add(food);
        }
        return foods;
    }

    public  void insertFood(Food food){
        ContentValues values = foodConvertToValue(food);
        values.remove(FoodTable._ID);
        db.insert(FoodTable.TABLE_NAME, FoodTable.NAME, values);
    }

    ContentValues foodConvertToValue(Food food){
        ContentValues values = new ContentValues();
            values.put(FoodTable._ID, food.getId());
        values.put(FoodTable.NAME, food.getName());
        values.put(FoodTable.PRICE, food.getPrice());
        values.put(FoodTable.TYPE, food.getType());
        return values;
    }
}
